package com.itheima.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class RecordTimeAspect {
    @Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    public void pt(){}
    //环绕通知
    @Around("pt()")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
            //目标方法执行
           Object object =  joinPoint.proceed();

            long end = System.currentTimeMillis();
            String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"方法执行了"+(end - start)+"毫秒" );
            log.info("{}方法执行了{}毫秒", methodName, (end - start));
            return object;
        }
        //前置通知
        @Before("pt()")
        public void before(){
            System.out.println("before....");
        }
        //目标方法执行后执行，无论方法正常结束还是异常结束
        @After("pt()")
        public void after(){
            System.out.println("after....");
        }
        //目标方法正常返回后执行，有异常不执行
        @AfterReturning("pt()")
        public void afterReturning(){
            System.out.println("afterReturning....");
        }
        //目标方法发生异常后才执行
        @AfterThrowing("pt()")
        public void afterThrowing(){
            System.out.println("afterThrowing....");
        }

        @Before("@annotation(com.itheima.annotation.OperationLog)")
        public void beforeAnnotation(){
            System.out.println("beforeAnnotation....");
        }

}
